package com.daafan.minilib;

import java.io.File;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import android.app.Application;
import android.graphics.Bitmap.CompressFormat;

import com.daafan.minilib.util.AppUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.update.UmengUpdateAgent;

import de.mindpipe.android.logging.log4j.LogConfigurator;

public class TushuApplication extends Application {

	private DisplayImageOptions	defaultOptions;
	private String				imageCacheDir	= AppUtils.getMyCacheDir("/imageCache/");

	@Override
	public void onCreate() {
		super.onCreate();

		AppUtils.mContext = getApplicationContext();

		configureImageLoader();

		// 初始化日志工具
		configureLogger();
	}

	/** 配置Logger，日志记录 */
	private void configureLogger() {
		LogConfigurator logConfigurator = new LogConfigurator();

		logConfigurator.setFileName(AppUtils.getMyCacheDir("logs") + File.separator + "log4j.txt");

		logConfigurator.setRootLevel(Level.DEBUG);

		logConfigurator.setLevel("org.apache", Level.ERROR);

		logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");

		logConfigurator.setMaxFileSize(1024 * 1024 * 5);

		logConfigurator.setImmediateFlush(true);

		try {
			logConfigurator.configure();
		} catch (Exception e) {
			e.printStackTrace();
		}

		Logger log = Logger.getLogger(getClass());

		log.info("My Application Created");
	}

	private void configureImageLoader() {
		defaultOptions = new DisplayImageOptions.Builder()

		// 之后需要修改此配置
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)

				.cacheInMemory(true).cacheOnDisc(true)

				.build();

		int screen_Height = getWallpaperDesiredMinimumHeight();
		int screen_Width = getWallpaperDesiredMinimumWidth();

		/**
		 * This configuration tuning is custom. You can tune every option, you may tune some of them, or you can create default
		 * configuration by ImageLoaderConfiguration.createDefault(this); method.
		 */
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())

				.threadPriority(Thread.NORM_PRIORITY)

				// .denyCacheImageMultipleSizesInMemory()

				.discCacheExtraOptions(screen_Width, screen_Height, CompressFormat.PNG, 90, null)
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).discCache(new UnlimitedDiscCache(new File(imageCacheDir)))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(200)

				.memoryCache(new LruMemoryCache(30 * 1024 * 1024)).memoryCacheSize(30 * 1024 * 1024)

				.tasksProcessingOrder(QueueProcessingType.LIFO)

				.defaultDisplayImageOptions(defaultOptions)

				// .writeDebugLogs()

				.build();
		ImageLoader.getInstance().init(config);
	}

	public DisplayImageOptions getDisplayOptions() {
		return defaultOptions;
	}

}
